//################################################################################################################################//
//##                                                                                                                            ##//
//## This Page is implement for create the  blog link                                                                           ##//
//##                                                                                                                            ##//
//## Author   : Manish Prajapati                                                                                                ##//
//## Dated    : 18-03-2006                                                                                                      ##//
//##                                                                                                                            ##//
//################################################################################################################################//
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteManager;
public partial class blogPost : System.Web.UI.Page
{// creating an instance of manager class
    Manager manager = new Manager(true,"ConnectionString");
    int blogId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            try
            {
                blogId = Convert.ToInt32(Request.QueryString["id"]);
                PostMessage1.TopicId = blogId;//set blogid value in TopicId property
            }
            catch { }
        }
        if (!Page.IsPostBack)
        {
            BindShowBlog();
        }
    }
    /// <summary>
    /// This function fire for fill the gridview control after post a message 
    /// </summary>
    /// <param name="writer"></param>
    protected override void Render(HtmlTextWriter writer)
    {
        if (Manager.GetPostBackControl(Page) == "btnSubmit")
        {
            BindShowBlog();
        }
        base.Render(writer);
    }
    /// <summary>
    /// Bind GridView from tbl_Messages behalf on topicid
    /// </summary>
    protected void BindShowBlog()
    {
        string SQLBlog = "SELECT Name FROM tbl_Topics WHERE isDelete=0 AND id = " + blogId;
        DataSet dsShowBlog = manager.ExecuteQuery(SQLBlog, "tbl_Topics");
        if (dsShowBlog.Tables[0].Rows.Count > 0)
        {
            lblBlogName.Text = dsShowBlog.Tables[0].Rows[0][0].ToString();
//            string SQL = @"SELECT tbl_Messages.Description AS msgDescription,tbl_Messages.DateAdded AS msgDate,
//            case usertype when 1	then (SELECT firstname +' '+lastname from tbl_admin WHERE adminid=tbl_Messages.userid) 
//            when 2 then (SELECT fname +' '+mname+' '+lname from tbl_Doctors WHERE id=tbl_Messages.userid)
//            when 3 then (SELECT fname +' '+mname+' '+lname from tbl_users WHERE id=tbl_Messages.userid)
//            end AS postedName FROM  tbl_Messages WHERE tbl_Messages.IsDelete=0 AND tbl_Messages.topicid = " + blogId + " ORDER BY tbl_Messages.DateAdded DESC";

            string SQL = @"SELECT tbl_Messages.Description AS msgDescription,tbl_Messages.DateAdded AS msgDate,
            case usertype when 1	then (SELECT 'Admin') 
            when 2 then (SELECT fname +' '+mname+' '+lname from tbl_Doctors WHERE id=tbl_Messages.userid)
            when 3 then (SELECT nickname from tbl_users WHERE id=tbl_Messages.userid)
            end AS postedName FROM  tbl_Messages WHERE tbl_Messages.IsDelete=0 AND tbl_Messages.topicid = " + blogId + " ORDER BY tbl_Messages.DateAdded DESC";

            DataSet ds = manager.ExecuteQuery(SQL, "tbl_Messages");
            if (ds.Tables[0].Rows.Count > 0)
            {
                grdShowBlog.DataSource = ds.Tables[0];
                grdShowBlog.DataBind();
                lblerr.Visible = false;
            }
            else
            {
                lblerr.Text = "<font color=red><b>No messages posted in this topic !!!</b></font>";
                lblerr.Visible = true;
            }
        }
        else // topic deleted
        {
            lblerr.Text = "<font color=red><b>Current topic has been deleted !!!</b></font>";
            lblerr.Visible = true;
        }
    }

    // paging
    protected void grdShowBlog_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdShowBlog.PageIndex = e.NewPageIndex;
        BindShowBlog();
    }
}
